package com.hehao.kurunner.ui;

import android.support.v4.view.ViewPager;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.hehao.kurunner.R;
import com.hehao.kurunner.adapter.ViewPagerAdapter;
import com.hehao.kurunner.common.BaseActivity;
import com.hehao.kurunner.common.BaseApplication;

import java.util.ArrayList;
import java.util.List;

/**
 * 引导页面
 * @author HeHao
 * @time 2015/11/13 15:14
 * @email 139940512@qq.com
 */
public class GuideActivity extends BaseActivity {

    private ViewPager viewPager;
    private List<View> views;

    private float startX;

    @Override
    protected void initView() {
        viewPager = new ViewPager(context);
        setContentView(viewPager, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
    }

    @Override
    protected void initData() {
        views = new ArrayList<>();
        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        ImageView img = null;
        img = new ImageView(context);
        img.setScaleType(ImageView.ScaleType.FIT_XY);
        img.setImageResource(R.mipmap.launch_bg);
        img.setLayoutParams(lp);
        views.add(img);

        img = new ImageView(context);
        img.setScaleType(ImageView.ScaleType.FIT_XY);
        img.setImageResource(R.mipmap.launch_bg);
        img.setLayoutParams(lp);
        views.add(img);

        img = new ImageView(context);
        img.setScaleType(ImageView.ScaleType.FIT_XY);
        img.setImageResource(R.mipmap.launch_bg);
        img.setLayoutParams(lp);
        views.add(img);

        viewPager.setAdapter(new ViewPagerAdapter(views));
        viewPager.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(viewPager.getCurrentItem() == 2){
                    switch (event.getAction()){
                        case MotionEvent.ACTION_DOWN:
                            startX = event.getX();
                            break;
                        case MotionEvent.ACTION_MOVE:
                            break;
                        case MotionEvent.ACTION_UP:
                            if(startX - event.getX() > 200){
                                toNextPage();
                            }
                            break;
                    }
                }
                return false;
            }
        });
    }

    private void toNextPage() {
        if(((BaseApplication)getApp()).isLogin() == null){
            startActExit(MainActivity.class);
        }else{
            startActExit(LoginActivity.class);
        }
    }
}
